package com.team.service.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.team.common.core.utils.DateUtils;
import com.team.service.domain.dto.ServiceItemAddDto;
import com.team.service.domain.dto.ServiceItemDto;
import com.team.service.domain.vo.ServiceItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.team.service.mapper.ServiceItemMapper;
import com.team.service.domain.ServiceItem;
import com.team.service.service.IServiceItemService;
import org.springframework.transaction.annotation.Transactional;
import com.team.service.annotation.CacheOperation;
import com.team.service.annotation.CacheSync;
import org.springframework.beans.BeanUtils;

/**
 * 服务管理Service业务层处理
 * 
 * @author panyue
 * @date 2025-07-09
 */
@Service
public class ServiceItemServiceImpl implements IServiceItemService 
{
    @Autowired
    private ServiceItemMapper serviceItemMapper;

    /**
     * 查询服务管理
     * 
     * @param id 服务管理主键
     * @return 服务管理
     */
    @Override
    @CacheSync(
        operation = CacheOperation.READ, 
        keyPrefix = "service:item", 
        key = "#id", 
        expireSeconds = 3600
    )
    public ServiceItemVo selectServiceItemById(Long id) {
        // 从数据库查询数据
        ServiceItemVo serviceItemVo = serviceItemMapper.selectServiceItemById(id);
        return serviceItemVo;
    }

    /**
     * 查询服务管理列表
     * 
     * @param serviceItemDto 服务管理
     * @return 服务管理
     */
    @Override
    public List<ServiceItemVo> selectServiceItemList(ServiceItemDto serviceItemDto)
    {
        return serviceItemMapper.selectServiceItemList(serviceItemDto);
    }

    /**
     * 新增服务管理
     */
    @Override
    @CacheSync(
        operation = CacheOperation.WRITE, 
        keyPrefix = "service:item", 
        key = "#result", // 使用返回的ID作为键
        expireSeconds = 3600,
        topic = "cache.sync.key"
    )
    public int insertServiceItem(ServiceItemAddDto serviceItemAddDto)
    {
        String categoryName=serviceItemAddDto.getCategoryName();
        String merchantName=serviceItemAddDto.getMerchantName();
        Long categoryId=serviceItemMapper.selectCategoryIdByName(categoryName);
        System.out.println(categoryId);
        Long merchantId=serviceItemMapper.selectMerchantIdByName(merchantName);
        System.out.println(merchantId);
        ServiceItem serviceItem=BeanUtil.toBean(serviceItemAddDto,ServiceItem.class);
        serviceItem.setCategoryId(categoryId);
        serviceItem.setMerchantId(merchantId);
        serviceItem.setCreateTime(DateUtils.getNowDate());
        serviceItemMapper.insertServiceItem(serviceItem);
        
        // 返回自增ID
        return serviceItem.getId().intValue();
    }

    /**
     * 修改服务管理
     */
    @Override
    @CacheSync(
        operation = CacheOperation.WRITE, 
        keyPrefix = "service:item", 
        key = "#serviceItemAddDto.serviceNo", 
        expireSeconds = 3600,
        topic = "cache.sync.key"
    )
    public int updateServiceItem(ServiceItemAddDto serviceItemAddDto)
    {
        String categoryName=serviceItemAddDto.getCategoryName();
        String merchantName=serviceItemAddDto.getMerchantName();
        Long categoryId=serviceItemMapper.selectCategoryIdByName(categoryName);
        System.out.println(categoryId);
        Long merchantId=serviceItemMapper.selectMerchantIdByName(merchantName);
        System.out.println(merchantId);
        ServiceItem serviceItem=BeanUtil.toBean(serviceItemAddDto,ServiceItem.class);
        serviceItem.setCategoryId(categoryId);
        serviceItem.setMerchantId(merchantId);
        serviceItem.setUpdateTime(DateUtils.getNowDate());
        return serviceItemMapper.updateServiceItem(serviceItem);
    }

    /**
     * 批量删除服务管理
     */
    @Override
    @CacheSync(
        operation = CacheOperation.WRITE, 
        keyPrefix = "service:item", 
        key = "#ids[0]", 
        clearOnError = true,
        topic = "cache.sync.key"
    )
    public int deleteServiceItemByIds(Long[] ids)
    {
        return serviceItemMapper.deleteServiceItemByIds(ids);
    }

    /**
     * 删除服务管理信息
     * 
     * @param id 服务管理主键
     * @return 结果
     */
    @Override
    public int deleteServiceItemById(Long id)
    {
        return serviceItemMapper.deleteServiceItemById(id);
    }

    @Override
    public List<String> selectMerchantList() {
        return serviceItemMapper.selectMerchantList();
    }

    @Override
    public List<String> selectServices() {
        return serviceItemMapper.selectServices();
    }
}
